package com.egg.demo.scheduled;

import cn.hutool.core.date.DateUtil;
import com.egg.demo.service.WebsocketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class WebsocketScheduled {


    @Autowired
    private SimpMessagingTemplate wsTemplate;
    @Autowired
    private WebsocketService websocketService;

    // 总人数 TOPIC
    String TOPIC_STATISTICAL_USER_TOTAL = "/statistical/user/total";
    // 在线人数 TOPIC
    String TOPIC_STATISTICAL_USER_ONLINE = "/statistical/user/online";


    //@Scheduled(cron = "0/45 * * * * ?")
    public void userTotal() {
        Integer userTotal = websocketService.userTotal();
        wsTemplate.convertAndSend(TOPIC_STATISTICAL_USER_TOTAL, userTotal);
        log.info("【推送总人数消息】时间: {} 人数: {}", DateUtil.formatDateTime(new Date()), userTotal);
    }

    //@Scheduled(cron = "0/30 * * * * ?")
    public void userOnline() {
        // 查询业务
        Integer userOnline = websocketService.userOnline();
        wsTemplate.convertAndSend(TOPIC_STATISTICAL_USER_ONLINE, userOnline);
        log.info("【推送在线人数消息】时间: {} 人数: {}", DateUtil.formatDateTime(new Date()), userOnline);
    }


}
